var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var base64 = require('../../utils/Base64.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    util: util,
    tc: 0,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    version: util.version,
    token: '',
    uid: 0,
    userinfo: {},
    web_view_url:"",
    screen_w: 320,
    catadata: [{ cid: 0, cname: "評論" }, 
              { cid: 1, cname: "活動" }, 
              { cid: 2, cname: "商機" }, 
              { cid: 3, cname: "我們" }],
    currcata:0,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          screen_w: res.windowWidth
        })
      },
    })

    var webUrl = base64.decode(options.weburl);
    this.setData({
      web_view_url: webUrl
    })
    console.log("网页地址：" + this.data.web_view_url);


    var tc = app.get_tc();
    that.setData({
      tc: tc,
    });
    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function (res) {
      //更新数据
      that.setData({
        loginUserInfo: res,
        uid: res.uid,
        web_view_url: that.data.web_view_url + "&uid="+res.uid,
        token: res.token
      })
      console.log("网页地址：" + that.data.web_view_url);
    }, inviteuid);

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          img_wh: (res.windowWidth - 30) / 2
        })
      },
    })


    wx.getSystemInfo({
      success: function (res) {

        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
        });

      }
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });
    //初始化底部导航
    var b_nav_data = app.getBottomNavData('', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });

    var username = options.username;
    wx.setNavigationBarTitle({
      title: username
    });
    if (util.orgid == '12545') {
      wx.setNavigationBarTitle({
        title: tc == 1 ? '線上直播間' : '线上直播间'
      });
    }
    this.setData({
      logo_url: options.logo_url ? options.logo_url : '',
      goodTypeId: options.cataid ? options.cataid : '',
      postuid: options.uid ? options.uid : '',
    });


    //读取登录用户信息
    app.LoginUserInfo(function (res) {
      if (res && res != -1) {
        that.setData({
          uid: res.uid,
          token: res.token
        });
      }
    }, '');
    

    that.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth,
      windowHeight: wx.getSystemInfoSync().windowHeight
    })

  },

  switchTab: function (e) {
    this.setData({
      currcata: e.detail.current
    });
    console.log("currcata", e.detail.current)
  },
  // 点击标题切换当前页时改变样式
  changeCata: function (e) {
    console.log("currcata", e.currentTarget.dataset.current)
    var cur = e.currentTarget.dataset.current;
    if (this.data.currcata == cur) { return false; }
    else {
      this.setData({
        currcata: cur
      })
    }
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },
})